﻿using Resn.WhuPub.BIL;
using Resn.WhuPub.DAL;
using Resn.WhuPub.DML;
using System;
using System.Diagnostics.Contracts;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现样书寄送流程的属性和方法
        /// </summary>
        public class BookDelivery : Entity,IBookDelivery
        {
                #region 构造函数

                /// <summary>
                /// 初始化联系人样书寄送流程
                /// </summary>
                /// <param name="model">样书寄送流程模型</param>
                public BookDelivery(BookDeliveryModel model)
                {
                        Contract.Requires(model != null, "样书寄送数据模型不能为空");
                        Model = model;
                }

                #endregion 构造函数

                #region 属性

                #region 基本属性

          

                /// <summary>
                /// 获取联系人标识
                /// </summary>
                /// <value>
                /// 联系人标识
                /// </value>
                public string ContactId
                {
                        get
                        {
                                Contract.Ensures(!string.IsNullOrEmpty(Model.ContactId), "联系人标识不能为空");
                                return Model.ContactId;
                        }
                }

                /// <summary>
                /// 获取或设置样书名称
                /// </summary>
                /// <value>
                /// 样书名称
                /// </value>
                public string BookId
                {
                        get
                        {
                                Contract.Ensures(!string.IsNullOrEmpty(Model.ContactId), "样书名称不能为空");
                                return Model.BookId;
                        }
                        set
                        {
                                Contract.Requires(!string.IsNullOrEmpty(Model.ContactId), "样书名称不能为空");
                                
                                        Model.BookId = value;
                        }
                }

                /// <summary>
                /// 获取或设置样书数量
                /// </summary>
                /// <value>
                /// 样书数量
                /// </value>
                [Query("样书数量")]
                public int Count
                {
                        get
                        {
                                Contract.Requires(Model.Count > 0, "样书数量不能为负数");
                                return Model.Count;
                        }
                        set
                        {
                                Contract.Requires(value > 0, "样书数量不能为负数");
                                
                                        Model.Count = value;
                                
                        }
                }

                /// <summary>
                /// 获取或设置寄送单号
                /// </summary>
                /// <value>
                /// 寄送单号
                /// </value>
                [Query("寄送单号")]
                public string DeliveryNumber
                {
                        get
                        {
                                Contract.Ensures(!string.IsNullOrEmpty(Model.DeliveryNumber), "寄送单号不能为空");
                                return Model.DeliveryNumber;
                        }
                        set
                        {
                                Contract.Requires(!string.IsNullOrEmpty(value), "寄送单号不能为空");
                                
                                        Model.DeliveryNumber = value;
                                
                        }
                }

                /// <summary>
                /// 获取或设置寄送时间
                /// </summary>
                /// <value>
                /// 寄送时间
                /// </value>
                [Query("寄送时间")]
                public DateTime DeliveryTime
                {
                        get
                        {
                                Contract.Requires(Model.DeliveryTime > DateTime.MinValue && Model.DeliveryTime < DateTime.MaxValue, "样书寄送时间超出范围");
                                return Model.DeliveryTime;
                        }
                        set
                        {
                                Contract.Requires(value > DateTime.MinValue && value < DateTime.MaxValue, "样书寄送时间超出范围");
                                
                                        Model.DeliveryTime = value;
                                
                        }
                }

                #endregion 基本属性

                /// <summary>
                /// 获取样书寄送模型
                /// </summary>
                /// <value>
                /// 样书寄送模型
                /// </value>
                public BookDeliveryModel Model { get; private set; }

                /// <summary>
                /// 获取联系人
                /// </summary>
                /// <value>
                /// 联系人
                /// </value>
                public IContact Contact
                {
                        get
                        {
                                var model = DBStorage.Instance.Select<ContactModel>(ContactId);
                                return new Contact(model);
                        }
                }

                /// <summary>
                /// 获取样书
                /// </summary>
                /// <value>
                /// 样书
                /// </value>
                public IBook Book
                {
                        get
                        {
                                var model = DBStorage.Instance.Select<BookModel>(ContactId);
                                return new Book(model);
                        }
                }

                /// <summary>
                /// 获取样书名称
                /// </summary>
                /// <value>
                /// 样书名称
                /// </value>
                [Query("样书名称")]
                public string BookName
                {
                        get { return Book.Name; }
                }



                #endregion 属性


                #region 方法

                /// <summary>
                /// 获取指定标识的对象
                /// </summary>
                /// <param name="id">标识</param>
                /// <returns></returns>
                public static BookDelivery Select(string id)
                {
                        return Select<BookDelivery, BookDeliveryModel>(id);
                }

                #endregion

        }
}